using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using NBusiness.CodeDom;
using NBusiness.Data.Rules;

namespace NBusiness.Data.Rules.Validation
{
    [Serializable]
    //[ExpectedArgumentType(typeof(MinLengthEventArgs))]
    public sealed class MinLengthRule : ValidationRule
    {
        public MinLengthRule(Type type, string field, MinLengthEventArgs arg)
            : base(type, field, ValidateMinLengthRule, arg, "Resources.MinLengthRuleMessage")
        { }

        public static bool ValidateMinLengthRule(object value, ValidationEventArgs e)
        {
            MinLengthEventArgs arg = e as MinLengthEventArgs;
            bool retVal = true;
            if (value != null)
            {
                retVal = false;
                IEnumerator enumerator = (value as IEnumerable).GetEnumerator();
                int length = 0;
                while (enumerator.MoveNext())
                {
                    length++;
                    if (length >= arg.Length)
                    {
                        retVal = true;
                        break;
                    }
                }
            }
            return retVal;
        }
    }
}
